#!/bin/sh

pwd=`pwd`
srcDir=$pwd/src
serverDir=$srcDir/server
targetDir="/data/code/project/go_execute/bin"
wsDir="/data/code/project/go_websocket/bin"

dbHost="192.168.1.241"
dbUser="work"
dbPass="TkkN3jT#urz^Mwgh"
dbName="bitkop"

checkAndBuild()
{
    folder=$1
    exe=$1
    dir=$serverDir/$folder

    if [ -d $dir ]; then
        target=$targetDir/$folder/$exe
        wsTarget=$wsDir/$folder/$exe
        cd $dir
        if [ $exe == "bitkop" ]; then
            go build -a -x -o $wsTarget
        else
            go build -a -x -o $target
        fi
    else
        echo "sorry, directory not exists, please check!"
    fi
}

rebuildDbStructs()
{
    tmpFile="github.com/go-xorm/cmd/xorm/templates/goxorm"
    toDir="global/structs"
    cd $srcDir
    xorm reverse -s mysql "${dbUser}:${dbPass}@tcp(${dbHost}:3306)/${dbName}?charset=utf8" github.com/go-xorm/cmd/xorm/templates/goxorm $toDir
}

buildServer()
{
    if [ $# -eq 0 ]; then
        for folder in `ls $serverDir`; do
            checkAndBuild $folder
        done
    else
        while [ "$#" -ge "1" ]; do
            if [ "$1" == "struct" -o "$1" == "structs" ]; then
                rebuildDbStructs
            else
                checkAndBuild $1
            fi
            shift
        done
    fi
}

buildServer $*
